package com.modules.api.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.common.response.ResponseVO;
import com.common.utils.R;
import com.modules.api.service.IAppService;
import com.modules.content.VO.ContentsVO;
import com.modules.content.entity.Contents;
import com.modules.content.service.IContentsService;
import com.modules.contentImages.entity.ContentImages;
import com.modules.contentImages.service.IContentImagesService;
import com.modules.groups.entity.Groups;
import com.modules.groups.service.IGroupsService;
import com.modules.user.entity.UserEntity;
import com.modules.version.entity.Version;
import com.modules.version.service.IVersionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * app 控制器类，用于app端口接口  发送验证码、登录、退出、校验token、获取最新版本
 */
@RestController
@RequestMapping("/api")
public class AppController {
	@Autowired
	private IAppService appService;
	@Autowired
	private IVersionService versionService;
	@Autowired
	private IGroupsService groupsService;

	/**
	 * 发送验证码
	 *
	 * @Param user 用户信息
	 */
	@PostMapping("/smsSend")
	public R smsSend(@RequestBody Map<String, Object> param) {
		return appService.smsSend2(param);
	}

	/**
	 * 登录
	 *
	 * @Param phoneNumber 账号
	 * @Param code 手机验证码
	 * @Param mac 设备mac地址
	 */
	@PostMapping("/userLogin")
	public R login(@RequestBody Map<String, Object> param) {
		return appService.login2(param);
	}

	/**
	 * 退出
	 *
	 * @Param phoneNumber 账号
	 */
	@PostMapping("/userLogout")
	public R loginOut(@RequestBody Map<String, Object> param) {
		return appService.loginOut(param);
	}

	/**
	 * 校验token
	 *
	 * @Param mac 设备mac地址
	 */
	@PostMapping("/verifyToken")
	public R verifyToken(@RequestBody Map<String, Object> param, HttpServletRequest request) {
		return appService.verifyToken2(param, request);
	}

	/**
	 * 获得最新版本信息
	 */
	@GetMapping("/getNewVersion")
	public R getEntityById() {
		Version version = versionService.getOne(new QueryWrapper<Version>().lambda()
				.orderByDesc(Version::getCreateTime)
				.last("limit 1"));
		return R.ok().put("data", version);
	}

	/**
	 * 获得所有分组
	 */
	@GetMapping("/getGroupList")
	public R getGroupList() {
		List<Groups> list = groupsService.list();
		return R.ok().put("data", list);
	}

	/**
	 * 获得某个分组下的所有贴图
	 */
	@PostMapping("/getContentByGroupId")
	public R getGroupById(@RequestBody Map<String, Object> param) {
		return appService.getContentByGroupId(param);
	}
}
